package com.byzk.p2p.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.BaseController;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.service.TenderService;
import com.byzk.p2p.admin.user.service.UserService;
import com.byzk.p2p.admin.web.service.ArticleService;
import com.byzk.p2p.admin.web.service.BannerService;
import com.byzk.p2p.admin.web.vo.ArticleVo;
import com.byzk.p2p.admin.web.vo.BannerVo;
import com.byzk.p2p.app.dto.TenderShow;

/**
 *系统操作
 * @author suker.zhou
 *
 */
@Controller
@RequestMapping("/app")
@Scope("prototype")
public class AppSysController extends BaseController{
	
	@Autowired
	private BannerService  bannerService;
	@Autowired
	private UserService userService;
	@Autowired
	private TenderService tenderService;
	@Autowired
	private ArticleService articleService;
	
	/**
	 * banner查询接口
	 * @param info 接受参数
	 * @throws Exception
	 */
	@RequestMapping(value = "/banner")
	public void banner( @ModelAttribute BannerVo info) throws Exception {
		
		super.printJSON(new JsonMsg(-1, "查询成功！",bannerService.bannerList(info)));
		return;
	}
	
	/**
	 * 网站投资详情数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/tender/info")
	public String tenderInfo() throws Exception{
		TenderShow indexShow=new TenderShow();
		//用户
		int count=userService.countAll();
		indexShow.setRegCount(count);
		//累计投资金额
		Double totalTenderAmount = tenderService.getAllTenderAmount(null,null);
		indexShow.setTotalTenderAmount(totalTenderAmount==null?0:totalTenderAmount);
		//赚取收益
		Double income=tenderService.getAllRecovedInterest();
		indexShow.setIncome(income==null?0:income);
		//昨日投资金额
		Double yesterdayTenderAmount = tenderService.getAllTenderAmount(DateUtils.getFirstSecondFewDaysAgo(1),DateUtils.getLastSecondFewDaysAgo(1));
		indexShow.setYesterdayAmount(yesterdayTenderAmount==null?0:yesterdayTenderAmount);	
		
		super.printJSON(new JsonMsg(-1, "操作成功", indexShow));
		return null;
	}
	
	/**
	 * 网站公告
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/notice")
	public String notice(@RequestParam(value="index",required=false,defaultValue="1") Integer index,
						 @RequestParam(value="limit",required=false,defaultValue="10") Integer limit) throws Exception{
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("status", 1);//已发布
		queryMap.put("type", "notice");
		
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap, index<=0?1:index, limit);
		
		super.printJSON(new JsonMsg(-1, "操作成功", pageInfo));
		return null;
	}
	
	/**
	 * 网站公告详情
	 * @param nid  公告id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/notice/detail")
	public String detail(@RequestParam("nid") Integer nid) throws Exception{
		ArticleVo vo=articleService.getArticleById(nid);
		super.printJSON(new JsonMsg(-1, "操作成功", vo));
		return null;
	}

}
